package com.fanketly.musicdiary.ui.components

import androidx.compose.foundation.Indication
import androidx.compose.foundation.IndicationInstance
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.*
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.platform.debugInspectorInfo
import com.fanketly.musicdiary.ui.theme.BlackBottomBackground
import com.fanketly.musicdiary.ui.theme.ClickColor
import com.fanketly.musicdiary.ui.theme.TextWhite


fun Modifier.clickableCustomIndication(
    enabled: Boolean = true,
    color: Color = ClickColor,
//    onClickLabel: String? = null,
//    role: Role? = null,
    onClick: () -> Unit
) = composed(
    inspectorInfo = debugInspectorInfo {
        name = "clickableCustomIndication"
        properties["enabled"] = enabled
        properties["color"] = color
//        properties["onClickLabel"] = onClickLabel
//        properties["role"] = role
        properties["onClick"] = onClick
    }
) {
    Modifier.clickable(enabled=enabled,interactionSource = remember { MutableInteractionSource() },
        indication = rememberRipple(color = color),
        onClick = { onClick.invoke() })
//    Modifier.clickable(
//        enabled = enabled,
//        onClickLabel = onClickLabel,
//        onClick = onClick,
//        role = role,
//        indication = indication,
//        interactionSource = remember { MutableInteractionSource() }
//    )
}

object GreyBlackIndication : Indication {

    private class WhiteIndicationInstance(
        private val isPressed: State<Boolean>,
        private val isHovered: State<Boolean>,
        private val isFocused: State<Boolean>,
    ) : IndicationInstance {
        override fun ContentDrawScope.drawIndication() {
            drawContent()
            if (isPressed.value) {
                drawRect(color = BlackBottomBackground.copy(alpha = 0.3f), size = size)
            } else if (isHovered.value || isFocused.value) {
                drawRect(color = BlackBottomBackground.copy(alpha = 0.1f), size = size)
            }
        }
    }

    @Composable
    override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
        val isPressed = interactionSource.collectIsPressedAsState()
        val isHovered = interactionSource.collectIsHoveredAsState()
        val isFocused = interactionSource.collectIsFocusedAsState()
        return remember(interactionSource) {
            WhiteIndicationInstance(isPressed, isHovered, isFocused)
        }
    }
}

object WhiteIndication : Indication {

    private class WhiteIndicationInstance(
        private val isPressed: State<Boolean>,
        private val isHovered: State<Boolean>,
        private val isFocused: State<Boolean>,
    ) : IndicationInstance {
        override fun ContentDrawScope.drawIndication() {
            drawContent()
            if (isPressed.value) {
                drawRect(color = TextWhite.copy(alpha = 0.3f), size = size)
            } else if (isHovered.value || isFocused.value) {
                drawRect(color = TextWhite.copy(alpha = 0.1f), size = size)
            }
        }
    }

    @Composable
    override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
        val isPressed = interactionSource.collectIsPressedAsState()
        val isHovered = interactionSource.collectIsHoveredAsState()
        val isFocused = interactionSource.collectIsFocusedAsState()
        return remember(interactionSource) {
            WhiteIndicationInstance(isPressed, isHovered, isFocused)
        }
    }
}
